using namespace std;
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

class Solution
{
public:
    vector<int> findSubstring(string s, vector<string>& words)
    {
        vector<int> rv;
        unordered_map<string, int> hashw;
        int len = words[0].size();
        for(const auto& e : words)
        {
            hashw[e]++;
        }
        int ws = words.size();

        for(int i=0; i<len; i++)
        {
            //要注意更新hashs和count
            int left = i;
            int right = i;
            unordered_map<string, int> hashs;
            int count = 0;
            while(right < s.size())
            {
                string in = s.substr(right, len);
                if(++hashs[in] <= hashw[in])
                    count++;
                if(right-left+1 > ws*len)
                {
                    string out = s.substr(left, len);
                    if(hashs[out]-- <= hashw[out])
                    {
                        count--;
                    }
                    left += len;
                }
                if(count == ws)
                {
                    rv.push_back(left);
                }
                right += len;
            }
        }
        return rv;
    }
};